package com.mc.moring.BafBase

import android.Manifest
import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.core.app.ActivityCompat
import com.gyf.immersionbar.ImmersionBar
import com.mc.moring.R
import com.mc.moring.util.InstallUtils
import com.mc.moring.widge.LoadingDialog
import com.nadia.totoro.view.IView

/**
 * author: Created by nao on 2018-09-11
 * version: 1.0.0
 */
abstract class BAFBaseActivity<T : Activity> : IBaseActivity<T>(), IView {

    open lateinit var loadDingDialog: LoadingDialog
    lateinit var mApplication: MyApplication
    lateinit var inputMethodManager: InputMethodManager

    abstract fun initParameter(bundle: Bundle?)

    abstract fun initLayout(): Int

    abstract fun afterInjectView()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置状态栏字体颜色
        mApplication = applicationContext as MyApplication
        loadDingDialog = LoadingDialog(this)

        initParameter(intent.extras)
        setContentView(initLayout())
        afterInjectView()
        initTopView()
        initImmersionBar()


//        if (abActionIBtn != null) abBackIBtn.setOnClickListener { finish() }
    }

    /**
     * 设置沉浸式
     */
    open fun initImmersionBar() {
        ImmersionBar.with(this)
            .fitsSystemWindows(true) //使用该属性,必须指定状态栏颜色
            .statusBarColor(R.color.white)
            .statusBarDarkFont(true)
            .init()
    }

    /**
     * 动态设置状态栏高度
     */
    fun initTopView() {
//        if (topView != null) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                topView.visibility = View.VISIBLE
//                //获取当前控件的布局对象
//                val params = topView.layoutParams as LinearLayout.LayoutParams
//                //设置当前控件布局的高度
//                params.height = StatusBarUtils.getStatusBarHeight(instance)
//                //将设置好的布局参数应用到控件中
//                topView.layoutParams = params
//            } else {
//                topView.visibility = View.GONE
//            }
//        }
    }

    /**
     * 设置状态栏 0 为白色
     */
    fun isActionBarBlackColor(isBlack: Boolean) {
//        window.decorView.systemUiVisibility = if (isBlack) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR else 0
    }

    fun requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            val mPermissionList = arrayOf(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.READ_LOGS,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,
                    Manifest.permission.GET_ACCOUNTS
            )
            ActivityCompat.requestPermissions(this, mPermissionList, 123)
        }
    }

    open fun showMyLoading() {
        if (loadDingDialog == null) {
            loadDingDialog = LoadingDialog(this)
        }
        if (!loadDingDialog.isShowing() && !isFinishing) { //如果没有显示，就要显示
            loadDingDialog.show()
        }
    }

    open fun hideMyLoading(){
        if (loadDingDialog != null && loadDingDialog.isShowing) {
            loadDingDialog.dismiss()
        }
    }

    override fun toastShowShort(msg: String) = toastShow(msg)

    override fun toastShowShort(rId: Int) = toastShow(rId)

    override fun showError(imageId: Int, text: String, status: Int) {

    }

    override fun showEmpty() {
    }

    override fun getContextView(): Activity = this

    /**
     * 用户相关
     */
    fun userId(): String = mApplication.userId

    fun userName(): String = mApplication.userName

    fun jumpQQ(context: Context, qq: String) = InstallUtils.jumpQQ(context, qq)

    fun requestPerm() {
//        AndPermission.with(this).permission(Permission.Group.MICROPHONE, Permission.Group.STORAGE, Permission.Group.CAMERA, Permission.Group.LOCATION).onGranted { }.onDenied { toastShowShort("拒绝麦克风和存储权限无法录像和录音，截屏"); }.start()
    }

    /**
     * 设置控件文本，判空
     */
    fun setTextToView(view: TextView, text: String) {
        view.text = if (text.isNullOrEmpty() || text == "null") "" else text
    }

    /**
     * 设置控件文本，判空
     */
    fun setTextToView(view: EditText, text: String) {
        view.setText(if (text.isNullOrEmpty() || text == "null") "" else text)
    }

    /**
     * 关闭键盘
     */
    fun hintKeyBoard() {
        //拿到InputMethodManager
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        //如果window上view获取焦点 && view不为空
        if (imm.isActive && currentFocus != null) {
            //拿到view的token 不为空
            if (currentFocus!!.windowToken != null) {
                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(currentFocus!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
            }
        }
    }
}
